﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Web.Configuration;
using System.Text;
using System.IO;

public partial class sitemap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        Response.ContentType = "text/xml";
        using (XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/sitemap.xml"), Encoding.UTF8)) {
            writer.WriteStartDocument();
            writer.WriteStartElement("urlset");
            writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

            string filename = "index.html";
            FileInfo f = new FileInfo(Server.MapPath("~/" + filename));
            writer.WriteStartElement("url");
            writer.WriteElementString("loc", "http://www.yzmanga.com/");
            writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", f.LastWriteTime));
            writer.WriteElementString("changefreq", "daily");
            writer.WriteElementString("priority", "1.0");
            writer.WriteEndElement();


            DirectoryInfo ldir = new DirectoryInfo(Server.MapPath("~/list"));
            foreach (FileInfo file in ldir.GetFiles())
            {
                writer.WriteStartElement("url");
                writer.WriteElementString("loc", "http://www.yzmanga.com/list/" + file.Name);
                writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", file.LastWriteTime));
                writer.WriteElementString("changefreq", "daily");
                writer.WriteElementString("priority", "0.5");
                writer.WriteEndElement();
            }

            foreach (DirectoryInfo cldir in ldir.GetDirectories())
            {
                foreach (FileInfo file in cldir.GetFiles())
	            {
		            writer.WriteStartElement("url");
                    writer.WriteElementString("loc", "http://www.yzmanga.com/list/" + cldir.Name + "/" + file.Name);
                    writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", file.LastWriteTime));
                    writer.WriteElementString("changefreq", "daily");
                    writer.WriteElementString("priority", "0.5");
                    writer.WriteEndElement();
	            }
            }

            DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/comic"));
            foreach (DirectoryInfo cdir in dir.GetDirectories())
            {
                FileInfo file = new FileInfo(Server.MapPath("~/comic/" + cdir.Name + "/" + filename));
                writer.WriteStartElement("url");
                writer.WriteElementString("loc", "http://www.yzmanga.com/comic/" + cdir.Name + "/" + filename);
                writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", file.LastWriteTime));
                writer.WriteElementString("changefreq", "monthly");
                writer.WriteElementString("priority", "0.5");
                writer.WriteEndElement();
            }
            writer.Close();
            Response.WriteFile(Server.MapPath("~/sitemap.xml"));
            Response.End();
        }
    }

    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    Response.Clear();
    //    YuuzleComic.Common.ServerInfo.BuildGoogleSitemap();
    //    Response.Redirect("http://www.google.com/webmasters/tools/ping?sitemap=http://www.yzmanga.com/sitemap.xml");
    //}
}